﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using UniversityManagementSystem.DAL;
using UniversityManagementSystem.Gateway;

namespace UniversityManagementSystem.Manager
{
    [Serializable]
    public class StudentManager
    {
        StudentGateway studentGateway = new StudentGateway();
        public string SaveStudentInformation(string registrationNo, Student aStudent, List<StudentEducation> aStudentEducation)
        {
            bool saveDone = false;
            string saveMessage = "";
            try
            {
                if(studentGateway.CheckEmail(aStudent.email))
                {
                    saveMessage = "Email alreadt exists,Please try another one.";
                }
                else
                {
                    saveDone = studentGateway.SaveAstudent(registrationNo, aStudent, aStudentEducation);
                    if (saveDone)
                    {
                        saveMessage = "Registration number of "+aStudent.name+" with "+aStudent.email+ " email is "+registrationNo;
                    }
                    else
                    {
                        saveMessage = "Unable to save student,Please try again later.";
                    }
                }
            }
            catch (Exception exception)
            {
                throw exception;
            }
            return saveMessage;
        }

        public bool GetUniqueKey(string randomNumber)
        {
            return studentGateway.GetUniqueKey(randomNumber);
        }

        public Student SearchAstudent(string regNo)
        {
            return studentGateway.SearchAstudent(regNo);
        }

        public string EnrollAcourse(string registrationNo, Course aCourse)
        {
            CourseGateway gateway = new CourseGateway();
            string enrollMessage = "";
            bool enrollDone = false;
            try
            {
                if (gateway.CheckCourseEnrolled(registrationNo, aCourse.CourseTitle))
                {
                    enrollMessage = "Student is already enrolled in this course.";
                }
                else
                {
                    enrollDone = gateway.EnrollAcourse(registrationNo, aCourse);
                    if (enrollDone)
                    {
                        enrollMessage = "Course has been enrolled successfully.";
                    }
                    else
                    {
                        enrollMessage = "Unable to enroll course.";
                    }
                }
            }
            catch (Exception exception)
            {
                throw new Exception("An error occured",exception);
            }
            return enrollMessage;
        }
    }
}